﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Enterprise.Service.Caiwubu;
using Enterprise.Control;
using Enterprise.Infrastructure;
using Enterprise.Model.CaiWu;
namespace Enterprise.UI.Web.Modules.CaiWuBu
{
    public partial class Index : System.Web.UI.Page
    {
        protected int year = (int)Utility.sink("year", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        protected string dw = (string)Utility.sink("dw", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (year == 0)
            {
                year = DateTime.Now.Year;
            }
            if (string.IsNullOrEmpty(dw))
            {
                dw = "SZK";
            }
            if (!IsPostBack)
            {
                bindDDL(); 
                OnStart();
                OnBindData();               
            }
        }

        private void OnStart()
        {
            CreateBT.AddButton("a.gif", "数据维护", "CWKHB.aspx", Infrastructure.Utility.PopedomType.Edit, HeadMenu1);
            CreateBT.AddButton("list.gif", "指标维护", "CwTypeIndex.aspx", Infrastructure.Utility.PopedomType.List, HeadMenu1);
        }

        private void OnBindData()
        {
            CwTypeService tService = new CwTypeService();
            CwZbService zService = new CwZbService();
            var tlist = tService.CwTypeList();
            List<CwZbEntity> list = new List<CwZbEntity>();
            foreach (CwTypeEntity entity in tlist)
            {
                list.AddRange(zService.CwZbList().Where(p => p.ZBTYPEID == entity.TYPEID));
            }
            GridView1.DataSource = list;
            GridView1.DataBind();

            Utility.GroupRows(GridView1, 0);
        }

        private void bindDDL()
        {
            int iNow = DateTime.Now.Year;
            int iPre = iNow - 2;
            int iNext = iNow + 2;
            for (int i = iPre; i <= iNext; i++)
            {
                ddl_ny.Items.Add(new ListItem(i.ToString()));
            }
            ListItem liyear = this.ddl_ny.Items.FindByValue(year.ToString());
            if (liyear != null)
                liyear.Selected = true;

            ListItem lidw = this.ddl_dwmc.Items.FindByValue(dw.ToString());
            if (lidw != null)
                lidw.Selected = true;
        }

        protected void ddl_ny_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectIndexChanged();
        }

        protected void ddl_dwmc_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectIndexChanged();
        }

        private void SelectIndexChanged()
        {
            int _year = (int)Utility.sink(ddl_ny.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Int);
            string _dw = (string)Utility.sink(ddl_dwmc.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
            Response.Redirect("Index.aspx?year=" + _year + "&dw=" + _dw + "");
            Response.End();
        }
    }
}